package com.game.league.task.detail;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.game.base.app.base.BaseActivity;
import com.game.base.glideV4.ImageLoader;
import com.game.base.utils.DecimalUtil;
import com.game.base.utils.toast.ToastUtil;
import com.game.base.view.dialog.DialogUtils;
import com.game.league.R;
import com.game.league.databinding.ActivityTaskDetailBinding;
import com.game.league.service.InterfaceMgr;
import com.game.league.task.detail.adapter.TaskTagAdapter;
import com.game.league.task.detail.panel.ExamplePanel;
import com.game.league.task.detail.panel.StepPanel;
import com.game.league.task.detail.panel.SubmitPanel;
import com.game.league.task.table.Task;
import com.game.league.util.Constants;
import com.game.league.util.SkipUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务详情/提交页
 * Created by Su on 2019/6/5.
 */
public class TaskDetailActivity extends BaseActivity<ActivityTaskDetailBinding> implements View.OnClickListener {
    private Task task = SkipUtil.currentTask;
    private TaskTagAdapter tagAdapter;

    private StepPanel stepPanel;
    private ExamplePanel examplePanel;
    private SubmitPanel submitPanel;

    public boolean isStartTask = false;  // 是否开始任务

    @Override
    protected int getLayoutId() {
        return R.layout.activity_task_detail;
    }

    @Override
    protected void initView() {
        binding.setTitle("任务详情");
        binding.setClick(this);

        if (!TextUtils.isEmpty(task.getIntroduction())) {
            binding.tvIntroduction.setText(task.getIntroduction());
        } else {
            binding.tvIntroduction.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(task.getDetailDescription())) {
            binding.tvDetailDescription.setText(task.getDetailDescription());
        } else {
            binding.tvDetailDescription.setVisibility(View.GONE);
        }

        // 任务标签
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.recyclerTag.setLayoutManager(layoutManager);
        tagAdapter = new TaskTagAdapter();
        binding.recyclerTag.setAdapter(tagAdapter);


        stepPanel = new StepPanel(this);
        binding.containerStep.addView(stepPanel.getContentView());

        examplePanel = new ExamplePanel(this);
        binding.containerExample.addView(examplePanel.getContentView());

        submitPanel = new SubmitPanel(this);
        binding.containerSubmit.addView(submitPanel.getContentView());
    }

    @Override
    protected void initData() {
        if (task == null) {
            ToastUtil.showShort("数据异常");
            return;
        }
        ImageLoader.loadRoundCenterCrop(mActivity, task.getImage(), binding.ivAppLogo);
        binding.tvAppName.setText(task.getTitle());
        binding.tvPrice.setText("+" + DecimalUtil.getFormatFloat(task.getPrice()));

        List<String> labels = new ArrayList<>();
        if (task.getLabel() != null && !task.getLabel().isEmpty()) {
            labels.addAll(task.getLabel());
        }
        if (task.getRemainNum() != Constants.TASK_MAX_NUM && task.getRemainNum() > 0) {
            labels.add(0, "剩余" + task.getRemainNum() + "份");
        }
        tagAdapter.setItems((ArrayList<String>) labels);

        stepPanel.refreshData();
        examplePanel.refreshData();
        submitPanel.refreshData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_iv:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        if (isStartTask) {
            DialogUtils.showMessagePositiveDialog(this, "", "确定要放弃任务吗？", (dialog, index) -> {
                InterfaceMgr.getInstance().resetRobTaskNum(task.getObjectId());
                super.finish();
            });
        } else {
            super.finish();
        }
    }
}
